
define(function(require, exports, module) {
    var $=require('zepto');
        cn=require('conment');
        api=require('api');
        var tel=/^1[3|4|5|7|8]\d{9}$/;
        var pswd=/^[0-9A-Za-z]+$/;
    module.exports = {
        isLogin:function (l) {
            var _this=this;
            cn.Ajax(api.isLogin,{
            },function(d){
                if(d.flag==1){
                    cn.setItem('user',JSON.stringify(d));
                    _this.userMoneyInfo(l)
                }
            },'post')

        },
        userMoneyInfo:function (l) {
            cn.Ajax(api.finance,{
            },function(d){
                if(d.flag==1){
                    cn.setItem('moneyInfo',JSON.stringify(d));
                    if(l){
                        return
                    }else {
                        window.location.href='/'
                    }

                }
            },'post')
        },
        userInfo:function () {
            var _this=this;
            var user=JSON.parse(cn.getItem('user'));
            var money=JSON.parse(cn.getItem('moneyInfo'));
            if(!user||!money){
                //home
                $('.show-logout').show();
                $('.un-login').show();
                $('#headGrzxA').on('click',function () {
                    window.location.href='/comview/login.html'
                });
                // console.log(cn.getCookie('COOKIE_CUSER'))
                if(!cn.getItem('clear_cookie')){
                    _this.loginOut(true);
                }
                return false
            }else if(user&&user.flag==1){
                //home
                $('.show-login').show();
                $('.login').show();
                $('.login_name').text(user.username);
                $('.nickname').text(user.nickname);
                $('#index-login-form-mb').text(parseFloat(money.available).toFixed(2));
                $('#shared_header_mb').text(parseFloat(money.available).toFixed(2)+'元');
                $('#sumMoney').text(parseFloat(money.available).toFixed(2)+'元');
                $('#headGrzxA').on('click',function () {
                    window.location.href='/comview/member.html'
                });
                $('#js-logout').on('click',function () {
                    _this.loginOut();
                });
                cn.Ajax(api.isLogin,{
                },function(d){
                    if(d.flag!=1){
                        localStorage.removeItem('moneyInfo');
                        localStorage.removeItem('user');
                        cn.deleteCookie('COOKIE_CUSER');
                    }
                },'post');
                return true
            }
        },
        loginOut:function (b) {
            var _this=this;
            cn.Ajax(api.logout,{
            },function(d){
                if(d.flag==1){
                    localStorage.removeItem('moneyInfo');
                    localStorage.removeItem('user');
                    cn.deleteCookie('COOKIE_CUSER');
                    cn.setItem('clear_cookie','true');
                    if(!b){
                        window.location.href='/'
                    }
                }
            },'post')
        },
        login:function (t,p) {
            var _this=this;
            if(t==''||t==null){
                pop.popup_err_msg({
                    text:'请输入用户名',
                    id:'#popup-p-error',
                });

            }else if(!tel.test(t)){
                pop.popup_err_msg({
                    text:'用户名输入格式错误',
                    id:'#popup-p-error',
                });
            }else{
                cn.Ajax(api.login,{
                    username :t,
                    password :p
                },function(d){
                    if(d.flag==1){
                        _this.isLogin()
                    }else{
                        pop.popup_err_msg({
                            text:d.message,
                            id:'#popup-p-error',
                        });

                    }
                },'post')
            }


        }
    }

});